home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2648 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.3 KB

  1. Path: engnews2.Eng.Sun.COM!usenet
  2. From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Multiple Abstract Classes Question
  5. Date: 18 Jan 1996 22:18:13 GMT
  6. Organization: SunSoft
  7. Message-ID: <NITIN.96Jan18141813@more.eng.sun.com>
  8. References: <1996Jan17.160425.6063@ned.cray.com>
  9. NNTP-Posting-Host: more.eng.sun.com
  10. In-reply-to: aztec@cray.com's message of 17 Jan 96 16:04:25 CST
  11.  
  12.  
  13. In article <1996Jan17.160425.6063@ned.cray.com> aztec@cray.com (Joel Garcia-Trevino  {x66457 CF/DEV}) writes:
  14.  
  15. > Newsgroups: comp.lang.c++
  16. > From: aztec@cray.com (Joel Garcia-Trevino  {x66457 CF/DEV})
  17. > Reply-To: aztec@cray.com
  18. > Organization: Cray Research, Inc.
  19. > Date: 17 Jan 96 16:04:25 CST
  20. >  Can I or is it a good idea to have multiple abstract classes to start building
  21. > a class hierarchy?
  22.  
  23. Yes, if you want to keep it flexible.  But if you design your
  24. classes properly, you can still keep them flexible without
  25. having the abstract class defined for your "type" class.  I
  26. don't even see a need for making your "base_net" class
  27. abstract!  I feel the methods get_name() & get_type(), don't
  28. need to be pure virtual at all.  These are accessor methods
  29. and if you keep the attributes they are accessing in the same
  30. class where these methods are defined, they don't need to be
  31. pure virtual methods.  This is how you can change your
  32. classes: 
  33.  
  34. 1.  Move attributes net::name and net::type_pntr to the class
  35.     base_net where the accessors are defined.
  36.  
  37. 2.  Change the accessor methods to return the corresponding
  38.     attribute.  You may not even have to make these methods
  39.     virtual but if you want the derived classes to redefine
  40.     these, you can keep them virtual.
  41.  
  42. 3.  Provide a constructor in the base_net class which takes
  43.     name and type as arguments.  Call this constructor from
  44.     the constructors of the derived class.  Provide default
  45.     values if you want.
  46.  
  47. 4.  Optionally, provide mutator methods to change the
  48.     attributes.  You could even combine the accessors and
  49.     mutators into one by returing a reference to the
  50.     attribute. 
  51.  
  52. 5.  If you still want the base_net class to be abstract (so
  53.     that nobody can instantiate it directly) and if you don't
  54.     have any other pure virtual methods, you can always put
  55.     the constructors for the base_net class in the protected
  56.     section so that only the derived classes can access them.
  57.  
  58. 6.  You may differ until later whether you really need
  59.     abstract class for type.  There won't be lot of changes
  60.     to do that.  So you could start with non-abstract version
  61.     first. 
  62.  
  63.  
  64. By defining the return type of the methods, you have already
  65. decided on the types of the attributes which your derived
  66. classes will have.  Why not then store them in the base class
  67. itself?  
  68.  
  69. Hope this helps.
  70.  
  71. -Nitin
  72.  
  73.  
  74.  
  75.  
  76. >   Example of the base (abstract) classes:
  77. >        class base_type; 
  78. >  
  79. >        class base_net {
  80. >         public:
  81. >          virtual char         *get_name() = 0;    // Gets the name
  82. >          virtual base_type    *get_type() = 0;    // Gets pointer to type
  83. >        };
  84. >        class base_type {
  85. >         public:
  86. >          virtual char *get_type() = 0;            // Gets type name 
  87. >        };
  88. >   The final classes would look something like:
  89. >       class type;
  90. >       class net: public base_net {
  91. >         private:
  92. >          char *name;
  93. >          type *type_pntr;
  94. >         public:
  95. >          char *get_name() { ..... };
  96. >          type *get_type() { ..... }; 
  97. >       };
  98. >      class type: public base_type {
  99. >        private:
  100. >         char *type_name;
  101. >        public:
  102. >         char *get_type() { ..... };
  103. >      };
  104. >  Does it make sense to do it this way?
  105. >         
  106. >  The reason why I want to do this is because there will be different types
  107. > of nets that I will need to work with. I don't know if there will be
  108. > different types of "type" but I thought why not make them all abstract. ???
  109. >  Thanks in advance for your help,
  110. >              Joel Garcia
  111. -- 
  112. ----------------------------------------------------------------------
  113. Nitin More                                                         
  114. SunSoft, Bldg 16  Off: (415) 786 7109                                 
  115. Menlo Park, CA    Fax: (415) 786 7957   e-mail: nitin@more.eng.sun.com
  116. ----------------------------------------------------------------------
  117.